import { ProTableValue } from '@/interface/public';

/**
 * @param { Array<any>} dataList 表格数据
 * @param { string} mergeField 合并的字段
 * @description 合并列
 */
export const mergeColumn = (dataList: any, mergeField: string, flag = 'rowSpan') => {
  const length = dataList.length;
  if (!length) {
    return [];
  }

  for (let i = 0; i < dataList.length; i++) {
    dataList[i][flag] = 1;
    for (let j = i + 1; j < dataList.length; j++) {
      if (dataList[i][mergeField] === dataList[j][mergeField]) {
        dataList[i][flag]++;
        if (!dataList[j][flag]) {
          dataList[j][flag] = 1;
        }
        dataList[j][flag]--;
      }
    }
    // 这里跳过已经重复的数据
    i = i + dataList[i][flag] - 1;
  }
};
/**
 * @param {any}  data 需要导出的对象
 * @param {String}  title 导出的标题
 * @returns 导出
 */
export const exportData = (data: BlobPart, title: string, flag: boolean = false) => {
  if (!data) return;
  const blob = new Blob([data], { type: 'application/vnd.ms-excel;charset=utf-8' });
  const url = window.URL.createObjectURL(blob);
  const aLink = document.createElement('a');
  aLink.style.display = 'none';
  aLink.href = url;
  aLink.setAttribute('download', `${title}${!flag ? '.xlsx' : ''}`);
  document.body.appendChild(aLink);
  aLink.click();
  document.body.removeChild(aLink);
  window.URL.revokeObjectURL(url);
};

export const _clearTableData = (arr: Array<ProTableValue>) => {
  for (let item of arr) {
    if (item.value) {
      item.value.tableData = [];
      item.value.originData = [];
    }
  }
};
